package packet;

import data.FData;

import java.io.ByteArrayOutputStream;
import java.io.DataOutputStream;
import java.io.IOException;

/**
 * Created by IntelliJ IDEA.
 * User: qiukx
 * Date: 2009-6-2
 * Time: 16:20:32
 */
public class PktCtlMsg implements FData {
    public static final int LEN_LENGTH = 4;
    public static final int CMD_LENGTH = 4;

    private final static int PACK_SIZE = 8;
    private final static int PACK_TYPE = FData.DATA_HEAD;
    private int len;
    private int cmd;

    public int getLen() {
        return len;
    }

    public void setLen(int len) {
        this.len = len;
    }

    public int getCmd() {
        return cmd;
    }

    public void setCmd(int cmd) {
        this.cmd = cmd;
    }

    public int getPackLeng() {
        return PACK_SIZE;
    }

    public int getDataType() {
        return PACK_TYPE;
    }

    public void cleanData() {
        //
    }

    public PktCtlMsg() {
    }

    public PktCtlMsg(int len, int cmd) {
        this.len = len;
        this.cmd = cmd;
    }

    public byte[] toBinaryData() {
        ByteArrayOutputStream buf = new ByteArrayOutputStream();
        DataOutputStream out = new DataOutputStream(buf);
        byte[] output = new byte[8];
        byte[] outputLen = null;
        byte[] outputCmd = null;
        try {
            out.writeInt(len);
            outputLen = buf.toByteArray();
            buf.reset();
            out.writeInt(cmd);
            outputCmd = buf.toByteArray();
        } catch (IOException e) {
            e.printStackTrace();
        }

        System.arraycopy(outputLen, 0, output, 0, LEN_LENGTH);
        System.arraycopy(outputCmd, 0, output, CMD_LENGTH, CMD_LENGTH);

        return output;
    }
}
